package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exceptions.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.LeaseAgreement;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.admin.mapper.LeaseAgreementMapper;
import com.atguigu.lease.web.admin.service.LeaseAgreementService;
import com.atguigu.lease.web.admin.vo.agreement.AgreementQueryVo;
import com.atguigu.lease.web.admin.vo.agreement.AgreementVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author liubo
 * @description 针对表【lease_agreement(租约信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
        implements LeaseAgreementService {

    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;

    @Override
    public void customPage(Page<AgreementVo> page, AgreementQueryVo queryVo) {
        leaseAgreementMapper.customSelectPage(page,queryVo);
    }

    @Override
    public AgreementVo customDetail(Long id) {
        AgreementVo agreementVo = leaseAgreementMapper.customSelectById(id);
        return agreementVo;
    }

    /*
       在租状态的租约不能删除!  2 5 7
     */
    @Override
    public void customRemove(Long id) {
        //先查询
        //根据id和状态查询
        LambdaQueryWrapper<LeaseAgreement> leaseAgreementLambdaQueryWrapper = new LambdaQueryWrapper<>();
        leaseAgreementLambdaQueryWrapper.eq(LeaseAgreement::getId,id).in(LeaseAgreement::getStatus,
                LeaseStatus.SIGNED,LeaseStatus.WITHDRAWING,LeaseStatus.RENEWING);
        LeaseAgreement leaseAgreement = getOne(leaseAgreementLambdaQueryWrapper);
        if (leaseAgreement != null){
            throw  new LeaseException(ResultCodeEnum.ILLEGAL_REQUEST.getMessage(),ResultCodeEnum.ILLEGAL_REQUEST.getCode());
        }
        removeById(id);
    }
}




